﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.Template;
using Csla.Wpf;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using OpenExpressApp.WPF.Controls;
using Itenso.Windows.Input;
using Csla;
using Csla.Core;
using OpenExpressApp.Module;
using OpenExpressApp.MetaModel;
using OpenExpressApp.Types;
using System.Windows.Controls.Primitives;
using AvalonDock;
using OpenExpressApp.WPF.Command;

namespace OpenExpressApp.Module.WPF.Templete
{
    /// <summary>
    /// 在ViewDataLoaderBase的基础上，添加控件的生成。
    /// 
    /// 即在数据加载的时候，显示一个不停转动的Busy控件。
    /// </summary>
    public abstract class ViewController : ViewDataLoaderBase, IControlWrapper
    {
        private Grid _control;

        public ViewController(WPFObjectView view)
            : base(view)
        {
            this.InitControl();
        }

        public new WPFObjectView View
        {
            get
            {
                return base.View as WPFObjectView;
            }
        }

        /// <summary>
        /// 生成的表格控件
        /// </summary>
        public Grid Control
        {
            get
            {
                return this._control;
            }
        }

        protected override CslaDataProvider CreateDataProvider()
        {
            return new GCslaDataProvider();
        }

        private void InitControl()
        {
            //生成busy控件
            var busy = new BusyAnimation
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Center,
                Width = 20,
            };

            //绑定TextBox到对象属性
            var bdIsRunning = new Binding("IsBusy");
            bdIsRunning.Source = this.DataProvider;
            bdIsRunning.BindsDirectlyToSource = true;
            busy.SetBinding(BusyAnimation.IsRunningProperty, bdIsRunning);

            //绑定IsVisible
            var bdIsVisible = new Binding("IsBusy");
            bdIsVisible.Source = this.DataProvider;
            bdIsVisible.BindsDirectlyToSource = true;
            bdIsVisible.Converter = new BooleanToVisibilityConverter();
            busy.SetBinding(BusyAnimation.VisibilityProperty, bdIsVisible);

            //生成表格控件
            this._control = new Grid();
            this._control.Children.Add(this.View.Control);
            this._control.Children.Add(busy);
        }

        #region IControlWrapper Members

        object IControlWrapper.Control
        {
            get
            {
                return this._control;
            }
        }

        #endregion
    }
}
